#!/bin/bash
#Time:2017-8-15 17:37:39
#Author: marisn
#Blog: blog.67cc.cn
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
ulimit -c 0
rm -rf ssv4*
clear
pass='blog.67cc.cn';
echo -e "请输入Marisn'blog地址:[\033[32m $pass \033[0m] "
read inputPass
if [ "$inputPass" != "$pass" ];then
    #网址验证
     echo -e "\033[31m很抱歉,输入错误\033[0m";
     exit 1;
fi;
function install_ss_panel_v4(){
yum -y remove httpd
yum install -y unzip zip git
wget -c https://git.oschina.net/marisn/ssv4/raw/master/lnmp1.3.zip && unzip lnmp1.3.zip && cd lnmp1.3 && chmod +x install.sh && ./install.sh lnmp
cd /root/ && wget http://download.redis.io/releases/redis-4.0.1.tar.gz && tar xzf redis-4.0.1.tar.gz && cd redis-4.0.1 && make
wget https://git.oschina.net/marisn/ssv4/raw/master/redis.conf -O /root/redis-4.0.1/redis.conf
cd /root/redis-4.0.1/src/ && ./redis-server ../redis.conf
cd /home/wwwroot/default/
rm -rf index.html
git clone https://git.oschina.net/marisn/ss-panel.git tmp && mv tmp/.git . && rm -rf tmp && git reset --hard
curl -sS https://install.phpcomposer.com/installer | php
chmod +x composer.phar
chmod -R 777 storage
cp .env.example .env
chattr -i .user.ini
mv .user.ini public
chown -R root:root *
chmod -R 777 *
chown -R www:www storage
chattr +i public/.user.ini
wget -N -P  /usr/local/nginx/conf/ http://home.ustc.edu.cn/~mmmwhy/nginx.conf 
service nginx restart
echo "cd /root/redis-4.0.1/src/ && ./redis-server ../redis.conf" >> /etc/rc.local
mysql -hlocalhost -uroot -proot --default-character-set=utf8<<EOF
create database sspanel;
use sspanel;
source /home/wwwroot/default/sj.sql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
flush privileges;
EOF

cd /home/wwwroot/default

php composer.phar install
# yum -y install vixie-cron crontabs
# rm -rf /var/spool/cron/root
# echo 'SHELL=/bin/bash' >> /var/spool/cron/root
# echo 'PATH=/sbin:/bin:/usr/sbin:/usr/bin' >> /var/spool/cron/root
# echo '0 0 * * * php /home/wwwroot/default/xcat dailyjob' >> /var/spool/cron/root
# echo '*/1 * * * * php /home/wwwroot/default/xcat checkjob' >> /var/spool/cron/root
# echo "*/1 * * * * php /home/wwwroot/default/xcat synclogin" >> /var/spool/cron/root
# echo "*/1 * * * * php /home/wwwroot/default/xcat syncvpn" >> /var/spool/cron/root
# echo '30 22 * * * php /home/wwwroot/default/xcat sendDiaryMail' >> /var/spool/cron/root
# /sbin/service crond restart
IPAddress=`wget http://members.3322.org/dyndns/getip -O - -q ; echo`;
	echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
	echo "# A key to build success, go to http://${IPAddress}~               #"
	echo "# One click Install ssrpanel successed                             #"
	echo "# Author: marisn          Ssrpanel:ssrpanel                        #"
	echo "# Blog: http://blog.67cc.cn/                                       #"
    echo "#默认账号：marisn@67cc.cn                                          #"
    echo "#默认密码：marisn                                                  #"
	echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
}
function install_ssr(){
	yum -y update
	yum -y install git 
	yum -y install python-setuptools && easy_install pip 
	yum -y groupinstall "Development Tools" 
	#512M chicks add 1 g of Swap
	dd if=/dev/zero of=/var/swap bs=1024 count=1048576
	mkswap /var/swap
	chmod 0644 /var/swap
	swapon /var/swap
	echo '/var/swap   swap   swap   default 0 0' >> /etc/fstab
	wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.13.tar.gz
	tar xf libsodium-1.0.13.tar.gz && cd libsodium-1.0.13
	./configure && make -j2 && make install
	echo /usr/local/lib > /etc/ld.so.conf.d/usr_local_lib.conf
	ldconfig
	yum -y install python-setuptools
	easy_install supervisor
    cd /root
	wget https://raw.githubusercontent.com/echo-marisn/ssrpanel-one-click-script/master/ssr-3.4.0.zip
	unzip ssr-3.4.0.zip
	cd shadowsocksr
	./setup_cymysql.sh
	./initcfg.sh
	wget -N -P /root/shadowsocksr/ https://raw.githubusercontent.com/echo-marisn/ssrpanel-one-click-script/master/user-config.json
	wget -N -P /root/shadowsocksr/ https://raw.githubusercontent.com/echo-marisn/ssrpanel-one-click-script/master/userapiconfig.py
	wget -N -P /root/shadowsocksr/ https://raw.githubusercontent.com/echo-marisn/ssrpanel-one-click-script/master/usermysql.json
	sed -i "s#Userip#${Userip}#" /root/shadowsocksr/usermysql.json
	sed -i "s#Dbuser#${Dbuser}#" /root/shadowsocksr/usermysql.json
	sed -i "s#Dbport#${Dbport}#" /root/shadowsocksr/usermysql.json
	sed -i "s#Dbpassword#${Dbpassword}#" /root/shadowsocksr/usermysql.json
	sed -i "s#Dbname#${Dbname}#" /root/shadowsocksr/usermysql.json
	sed -i "s#UserNODE_ID#${UserNODE_ID}#" /root/shadowsocksr/usermysql.json
	yum -y install lsof lrzsz
	yum -y install python-devel
	yum -y install libffi-devel
	yum -y install openssl-devel
	yum -y install iptables
	systemctl stop firewalld.service
	systemctl disable firewalld.service
}
function install_node(){
	clear
	echo
    echo -e "\033[31m Add a node...\033[0m"
	echo
	sed -i '$a * hard nofile 512000\n* soft nofile 512000' /etc/security/limits.conf
	[ $(id -u) != "0" ] && { echo "Error: You must be root to run this script"; exit 1; }
	echo -e "You can go back to the car if you don't know"
	echo -e "If the connection fails, check that the database remote access is open"
	read -p "Please enter your docking database IP (enter default Local IP address) :" Userip
	read -p "Please enter the database name (enter default ssrpanel):" Dbname
	read -p "Please enter the database port (enter default 3306):" Dbport
	read -p "Please enter the database account (enter default root):" Dbuser
	read -p "Please enter your database password (enter default root):" Dbpassword
	read -p "Please enter your node number (enter default 1):  " UserNODE_ID
	IPAddress=`wget http://members.3322.org/dyndns/getip -O - -q ; echo`;
	Userip=${Userip:-"${IPAddress}"}
	Dbname=${Dbname:-"ssrpanel"}
	Dbport=${Dbport:-"3306"}
	Dbuser=${Dbuser:-"root"}
	Dbpassword=${Dbpassword:-"root"}
	UserNODE_ID=${UserNODE_ID:-"1"}
	install_ssr
    # 启用supervisord
	echo_supervisord_conf > /etc/supervisord.conf
	sed -i '$a [program:ssr]\ncommand = python /root/shadowsocksr/server.py\nuser = root\nautostart = true\nautorestart = true' /etc/supervisord.conf
	supervisord
	#iptables
	iptables -F
	iptables -X  
	iptables -I INPUT -p tcp -m tcp --dport 22:65535 -j ACCEPT
	iptables -I INPUT -p udp -m udp --dport 22:65535 -j ACCEPT
	iptables-save >/etc/sysconfig/iptables
	iptables-save >/etc/sysconfig/iptables
	echo 'iptables-restore /etc/sysconfig/iptables' >> /etc/rc.local
	echo "/usr/bin/supervisord -c /etc/supervisord.conf" >> /etc/rc.local
	chmod +x /etc/rc.d/rc.local
	touch /root/shadowsocksr/ssserver.log
	chmod 0777 /root/shadowsocksr/ssserver.log
	cd /home/wwwroot/default/storage/app/public/
	ln -S ssserver.log /root/shadowsocksr/ssserver.log
    chown www:www ssserver.log
	chmod 777 -R /home/wwwroot/default/storage/logs/
	clear
	echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
	echo "# Add success to the node and log on to the front site             #"
	echo "# Restart the envoy's point of entry into force...                 #"
	echo "# Author: marisn          Ssrpanel:ssrpanel                        #"
	echo "# Blog: http://blog.67cc.cn/                                       #"
	echo "# Github: https://gitee.com/marisn/ssv4/                           #"
	echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
	reboot
}
function install_BBR(){
     wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh&&chmod +x bbr.sh&&./bbr.sh
}
function install_RS(){
     wget -N --no-check-certificate https://github.com/91yun/serverspeeder/raw/master/serverspeeder.sh && bash serverspeeder.sh
}
echo -e "\033[31m#############################################################\033[0m"
echo -e "\033[32m#欢迎使用一键SS-panel V4搭建脚本 and 节点添加               #\033[0m"
echo -e "\033[34m#Blog: http://blog.67cc.cn/                                 #\033[0m"
echo -e "\033[35m#请选择你要搭建的脚本：                                     #\033[0m"
echo -e "\033[36m#1.  一键SS-panel V4搭建                                    #\033[0m"
echo -e "\033[37m#2.  一键添加SS-panel节点                                   #\033[0m"
echo -e "\033[36m#3.  一键  BBR加速  搭建                                    #\033[0m"
echo -e "\033[35m#4.  一键锐速破解版搭建                                     #\033[0m"
echo -e "\033[34m#                              PS:建议先搭建加速再搭建      #\033[0m"
echo -e "\033[33m#                                                           #\033[0m"
echo -e "\033[31m#############################################################\033[0m"
echo
read num
if [[ $num == "1" ]]
then
install_ss_panel_v4
fi;
if [[ $num == "2" ]]
then
install_node
fi;
if [[ $num == "3" ]]
then
install_BBR
fi;
if [[ $num == "4" ]]
then
install_RS
fi;